module.exports = class extends think.Model {
  queryByUserIds(idArr, fields = '*') {
    return this.where({id: ['in', idArr]}).field(fields).select();
  }
  queryByOpenId(openid, field = '*') {
    return this.where({
      openid
    }).field(field).find();
  }
  queryById(userId, field = '*') {
    return this.where({
      id: userId
    }).field(field).find();
  }
  hasExistedByOpenId(openid) {
    return this.where({
      openid
    }).count() > 0;
  }
  hasExistedByTelephone(telephone) {
    return this.where({
      telephone
    }).count() > 0;
  }
  queryByTelephone(telephone, field = '*') {
    return this.where({
      telephone
    }).field(field).find();
  }
  create(user) {
    user.create_time = think.datetime();
    return this.add(user);
  }
  updateUserInfoById(id, info) {
    think.logger.info('update user info ', info);
    info.modify_time = think.datetime();
    return this.updateById(id, info);
  }
  updateById(id, user) {
    return this.where({id}).update(user);
  }
  async findOrCreateByOpenId(openid) {
    let userId = await this.where({openid}).getField('id', true);
    if (!userId) {
      userId = await this.add({
        openid,
        create_time: think.datetime()
      });
    }
    return userId;
  }
};
